<?php

/**
 * Solicitud form base class.
 *
 * @method Solicitud getObject() Returns the current form's model object
 *
 * @package    BecArTe
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseSolicitudForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                  => new sfWidgetFormInputHidden(),
      'fecha'               => new sfWidgetFormDate(),
      'estado'              => new sfWidgetFormChoice(array('choices' => array('En Presentación' => 'En Presentación', 'Vencida' => 'Vencida', 'En Evaluación' => 'En Evaluación', 'Aprobada' => 'Aprobada', 'Rechazada' => 'Rechazada'))),
      'observacion'         => new sfWidgetFormTextarea(),
      'alumno_id'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('alumno'), 'add_empty' => false)),
      'beca_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('beca'), 'add_empty' => false)),
      'created_at'          => new sfWidgetFormDateTime(),
      'updated_at'          => new sfWidgetFormDateTime(),
      'created_by'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'add_empty' => true)),
      'updated_by'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Updator'), 'add_empty' => true)),
      'version'             => new sfWidgetFormInputText(),
      'acreditaciones_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Acreditacion')),
    ));

    $this->setValidators(array(
      'id'                  => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'fecha'               => new sfValidatorDate(),
      'estado'              => new sfValidatorChoice(array('choices' => array(0 => 'En Presentación', 1 => 'Vencida', 2 => 'En Evaluación', 3 => 'Aprobada', 4 => 'Rechazada'), 'required' => false)),
      'observacion'         => new sfValidatorString(array('max_length' => 1000, 'required' => false)),
      'alumno_id'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('alumno'))),
      'beca_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('beca'))),
      'created_at'          => new sfValidatorDateTime(),
      'updated_at'          => new sfValidatorDateTime(),
      'created_by'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'required' => false)),
      'updated_by'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Updator'), 'required' => false)),
      'version'             => new sfValidatorInteger(array('required' => false)),
      'acreditaciones_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Acreditacion', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('solicitud[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Solicitud';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['acreditaciones_list']))
    {
      $this->setDefault('acreditaciones_list', $this->object->acreditaciones->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveacreditacionesList($con);

    parent::doSave($con);
  }

  public function saveacreditacionesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['acreditaciones_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->acreditaciones->getPrimaryKeys();
    $values = $this->getValue('acreditaciones_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('acreditaciones', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('acreditaciones', array_values($link));
    }
  }

}
